﻿using System.Collections.Generic;
using DtoModels;
using Entities;
using IBasicsDal;
using SqlSugar;
using SqlSugarCore.Dal.BaseRealize;

namespace SqlSugarCore.Dal
{
    public class SysUserDal : BaseDal<sys_user>,ISysUserDal
    {
        private readonly SqlSugarClient _sqlSugarClient;

        public SysUserDal(SqlSugarClient sqlSugarClient) : base(sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        /// <summary>
        /// 分页加载所有用户信息
        /// </summary>
        /// <param name="pageIndex">指定页号</param>
        /// <param name="pageSize">指定一页数据的条数</param>
        /// <param name="totalCount">查询到的数据总条数</param>
        /// <returns></returns>
        public List<LoadAllUserResDto> LoadAllUser(int pageIndex, int pageSize, ref int totalCount)
        {
            var queryResult = this._sqlSugarClient.Queryable<sys_user, sys_dept, sys_user>((su, sd, su2) => new JoinQueryInfos(JoinType.Left, su.deptid == sd.id, JoinType.Left, su.mgr == su2.id))
                .OrderBy(su => su.id)
                .Select((su, sd, su2) => new LoadAllUserResDto
                {
                    id = SqlFunc.GetSelfAndAutoFill(su.id),
                    deptname = sd.title,
                    leadername = su2.name
                })
                .ToPageList(pageIndex, pageSize, ref totalCount);

            return queryResult;
        }

        
    }
}